package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.DailyCollect;
import cn.wolfcode.trip.base.domain.TravelCollect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.DailyQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyCollectService;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dailies")
public class DailyController {
    @Autowired
    private IDailyService dailyService;
    @Autowired
    private IDailyCollectService dailyCollectService;


    //根据状态查询日报
    @GetMapping
    public Object selectByState(DailyQuery qo) {
        return dailyService.query(qo);
    }

    @GetMapping("/state")
    public Object getState(DailyQuery qo) {
        return dailyService.query(qo);
    }

    //根据id 查询日报详情
    @GetMapping("/{id}")
    public Object getTravelById(@PathVariable Long id) {
        return dailyService.getDaily(id);
    }

    //查询收藏的攻略
    @GetMapping("/{id}/collect")
    public Object selectTavelsByuserId(QueryObject qo, @PathVariable Long id) {

        return dailyService.selectDailysByUserId(qo, id);
    }

    //收藏
    @PostMapping("/collect")
    public Object collect(Long dailyId) {
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询点赞记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user == null) {
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
            Long userId = user.getId();
            DailyCollect dailyCollect = dailyCollectService.get(userId, dailyId);

            if (dailyCollect != null) {
                dailyCollectService.delete(dailyCollect.getId());
            } else {
                DailyCollect collect = new DailyCollect();
                collect.setUser(user);
                collect.setDaily(dailyService.getDaily(dailyId));
                dailyCollectService.save(collect);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }


        return jsonResult;
    }

    @GetMapping("/get")
    //回显收藏的日报
    public Object selectCollect(Long dailyId) {

        JSONResult jsonResult = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            if (user == null) {
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
            return dailyCollectService.get(user.getId(), dailyId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }


}
